<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script>
      var data = [
        {
          id: 1,
          gname: "家电",
          goods: [
            {
              id: 11,
              gname: "冰箱",
              goods: [
                {
                  id: 111,
                  gname: "海尔",
                },
                {
                  id: 112,
                  gname: "美的",
                },
              ],
            },
            {
              id: 12,
              gname: "洗衣机",
            },
          ],
        },
        {
          id: 2,
          name: "服饰",
        },
      ];
      // 输入id号，返回数据对象
      function getID(json, id) {
        json.forEach(function (item) {
          //   console.log(item); //两个数组元素
          if (item.id === id) {
            // console.log(item);
            o = item;
            // 里层的数据 11 12 可以利用递归函数
            // 里面应该有goods这个数组并且数组的长度不为0
          } else if (item.goods && item.goods.length > 0) {
            o = getID(item.goods, id);
          }
        });
        return o;
      }
      console.log(getID(data, 1));
      console.log(getID(data, 11));
    </script>
  </head>
  <body></body>
</html>
